package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Holds a method to do the List sources command for WattDepot.
 * 
 * @author Kendyll Doi
 * @author Kimberly Heu
 */
public class WattSources {
  /**
   * Returns a list of sources with information of parent and description.
   * 
   * @param client the command line which has the sources.
   * @return a string with all the information.
   */
  public static String wattSources(WattDepotClient client) {
    StringBuffer buf = new StringBuffer(400);
    String output = null;
    String layout = "|%1$-17s|%2$-17s|%3$-100s\n";
    List<Source> sourceList = new ArrayList<Source>();
    try {
      sourceList = client.getSources();
    }
    catch (WattDepotClientException e) {
      System.err.println("There was a problem accessing the server: " + e.toString());
    }
    buf.append(String.format(layout, "Name", "Parent", "Description"));
    for (Source source : sourceList) {
      String Parent = "";
      if (source.getName().matches("[a-zA-Z_]*+[0-9]$")) {
        Parent = source.getName().substring(0, source.getName().indexOf("_", 4));
      }
      else if (source.getName().equalsIgnoreCase("SIM_OAHU_GRID")) {
        Parent = "NONE";
      }
      else {
        Parent = "SIM_OAHU_GRID";
      }
      buf.append(String.format(layout, source.getName(), Parent, source.getDescription()));
    }
    output = buf.toString();
    return output;
  }
}
